import { ActionContext } from "vuex";
import type { Counter, State } from "../index";

export default {
  // 不开启命名空间，action和mutation会共享一个命名空间，就可能会导致冲突
  // 开启命名空间：所有内容都在不同的命名空间，就不会命名冲突
  namespaced: true,
  state: {
    count: 0,
  },
  getters: {
    oddOrEven(state: Counter) {
      return state.count % 2 === 0 ? "偶数" : "奇数";
    },
  },
  actions: {
    increment({ commit }: ActionContext<Counter, State>, num: number) {
      commit("INCREMENT", num);
    },
  },
  mutations: {
    INCREMENT(state: Counter, num: number) {
      state.count += num;
    },
    DECREMENT(state: Counter, num: number) {
      state.count -= num;
    },
  },
};
